package org.tong.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.tong.bean.OrderItem;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class OrderItemDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    public List<OrderItem> queryAll() {
        return jdbcTemplate.query("select * from t_order_item", getRowMapper());
    }


    private RowMapper getRowMapper() {
        return new RowMapper<OrderItem>() {
            @Override
            public OrderItem mapRow(ResultSet rs, int rowNum) throws SQLException {
                OrderItem orderItem = new OrderItem();
                orderItem.setOrderId(rs.getBigDecimal("order_id").toBigInteger());
                orderItem.setOrderNo(rs.getString("order_no"));
                orderItem.setItemName(rs.getString("item_name"));
                orderItem.setPrice(rs.getBigDecimal("price"));
                return orderItem;
            }
        };
    }

    public int insert(OrderItem orderItem) {
        return jdbcTemplate.update("insert into t_order_item(order_id, order_no, item_name, price)values(?,?,?,?)", orderItem.getOrderId(), orderItem.getOrderNo(), orderItem.getItemName(), orderItem.getPrice());
    }
}
